package com.wanglei.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

@Configuration
@EnableSwagger2 //开启Swagger2
//@EnableOpenApi //开启Swagger3
public class SwaggerConfig {

    @Bean
    public Docket docket1(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("A");
    }

    @Bean
    public Docket docket2(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("B");
    }

    @Bean
    public Docket docket3(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("C");
    }

    @Bean
    public Docket docket(Environment environment){

        //设置要显示的Swagger环境
        Profiles profiles = Profiles.of("dev","test");

        //获取当前项目的环境 并判断是否要显示Swagger
        boolean flag = environment.acceptsProfiles(profiles);

        //配置了Swagger的Docket的Bean实例
//        DocumentationType documentationType = new DocumentationType();
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                //RequestHandlerSelectors,配置要扫描接口的方式
                //basePackage指定扫描的包
                //any扫描全部的包
                //none()都不扫描
                //withClassAnnotation()扫描类上的注解，参数是注解的反射对象
                //withMethodAnnotation()扫描方法上的注解
                .apis(RequestHandlerSelectors.basePackage("com.wanglei.controller"))
                //paths()，过滤路径，只让/kuang开头的请求通过
//                .paths(PathSelectors.ant("/kuang/**"))
                .build()
                .enable(true); //enable是否启动swagger，如果为false，则swagger不能在浏览器中访问
    }


    //配置swagger信息需要apiInfo类
    private ApiInfo apiInfo(){
        //作者信息
        Contact contact = new Contact("wl","https://www.baidu.com/","2459405142@qq.com");
        return new ApiInfo(
                "wl的Swagger日记",
                "本人描述",
                "v1.0",
                "https://www.baidu.com/",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());
    }
}
